﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Animation;

using SBPweb.RIA.Client.Contracts.Base;
using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.RIA.Client.Controls.Extensions
{
    
    public static class PersistencytExtentions
    {

        #region Internal fields

        internal static int ObjectId = 0;

        #endregion

        #region Store

        public static void StorePersistedProperty<T>(this UserControlBase element, string propertyName, T value)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
            {
                return;
            }
            ExtendableApplication.Current.Settings.Put<T>(GetPersistencyKey(element, propertyName), value);
        }

        public static void StorePersistedProperty<T>(this ContentControlBase element, string propertyName, T value)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
            {
                return;
            }
            ExtendableApplication.Current.Settings.Put<T>(GetPersistencyKey(element, propertyName), value);
        }

        //public static void StorePersistedProperty<T>(this UserControlBase element, DependencyProperty property, T value)
        //{
        //    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
        //    {
        //        return;
        //    }
        //    ExtendableApplication.Current.Settings.Put<T>(GetPersistencyKey(element, property), value);
        //}

        //public static void StorePersistedProperty<T>(this ContentControlBase element, DependencyProperty property, T value)
        //{
        //    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
        //    {
        //        return;
        //    }
        //    ExtendableApplication.Current.Settings.Put<T>(GetPersistencyKey(element, property), value);
        //}

        #endregion

        #region Retreive

        public static T RetreivePersistedProperty<T>(this UserControlBase element, string propertyName)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
            {
                return default(T);
            }
            return ExtendableApplication.Current.Settings.Get<T>(GetPersistencyKey(element, propertyName));
        }

        public static T RetreivePersistedProperty<T>(this ContentControlBase element, string propertyName)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
            {
                return default(T);
            }
            return ExtendableApplication.Current.Settings.Get<T>(GetPersistencyKey(element, propertyName));
        }

        //public static T RetreivePersistedProperty<T>(this UserControlBase element, DependencyProperty property)
        //{
        //    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
        //    {
        //        return default(T);
        //    }
        //    return ExtendableApplication.Current.Settings.Get<T>(GetPersistencyKey(element, property));
        //}

        //public static T RetreivePersistedProperty<T>(this ContentControlBase element, DependencyProperty property)
        //{
        //    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime || !ExtendableApplication.Current.IsSettingsPersistencyEnabled)
        //    {
        //        return default(T);
        //    }
        //    return ExtendableApplication.Current.Settings.Get<T>(GetPersistencyKey(element, property));
        //}

        #endregion

        #region ObjectIds

        internal static object GetObjectId(this UIElement element, ref object oId, string name)
        {
            if (oId == null || string.IsNullOrEmpty(oId.ToString()))
            {
                if (name != null && !string.IsNullOrEmpty(name.ToString()))
                {
                    return oId = name;
                }
                oId = string.Format("{0}{1}", element.GetType().Name, ObjectId++);
            }
            return oId;
        }

        #endregion

        #region Helper functions

        private static string GetPersistencyKey(UserControlBase element, string propertyName)
        {
            return string.Format("{0}.{1}.{2}", element.GetType().FullName, element.ObjectId, propertyName);
        }

        private static string GetPersistencyKey(ContentControlBase element, string propertyName)
        {
            return string.Format("{0}.{1}.{2}", element.GetType().FullName, element.ObjectId, propertyName);
        }

        //private static string GetPersistencyKey(UserControlBase element, DependencyProperty property)
        //{
        //    return string.Format("{0}.{1}.{2}", element.GetType().FullName, element.ObjectId, property.GetName());
        //}

        //private static string GetPersistencyKey(ContentControlBase element, DependencyProperty property)
        //{
        //    return string.Format("{0}.{1}.{2}", element.GetType().FullName, element.ObjectId, property.GetName());
        //}

        #endregion

    }

}